/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Console;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Kenneth Veenstra <kenneth.veenstra@gmail.com>
 */
public class FileReadWriter
{
    private ArrayList<String> que;
    
    public FileReadWriter()
    {
        que = new ArrayList<String>();
    }
    
    public List<String> getSqlList()
    {
        ArrayList<String> list = new ArrayList<String>();
        BufferedReader br = null;
        String line;
        String stack = "";
        try
        {
            br = new BufferedReader(new FileReader(FileReadWriter.class.getProtectionDomain().getCodeSource().getLocation().getPath() + "/database.sql"));
            //br = new BufferedReader(new FileReader("database.sql"));
            while ( (line = br.readLine()) != null )
            {
                if ( line.endsWith(";") )
                {
                    list.add(stack + " " + line);
                    stack = "";
                }
                else
                {
                    stack += " " + line;
                }
            }
        }
        catch ( IOException ex )
        {
            ex.printStackTrace();
        }
        finally
        {
            try
            {
                if ( br != null )
                {
                    br.close();
                }
            }
            catch ( IOException ex )
            {
                ex.printStackTrace();
            }
        }
        return list;
    }

    public void writeFile(String line, String destinationFile, Boolean append)
    {
        try
        {
            FileWriter fr = new FileWriter(destinationFile, append);
            BufferedWriter br = new BufferedWriter(fr);
            PrintWriter out = new PrintWriter(br);
            while(!que.isEmpty())
            {
                out.println(que.get(0));
                que.remove(0);
            }
            out.println(line);
            fr.close();
            br.close();
            out.close();
        }
        catch ( IOException e )
        {
            que.add(line);
        }
    }
}
